import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pornhub_app/components/ad_banner/ad_banner.dart';
import 'package:pornhub_app/components/app_bg_view.dart';
import 'package:pornhub_app/components/image_view.dart';
import 'package:pornhub_app/components/no_more/no_data.dart';
import 'package:pornhub_app/components/text_view.dart';
import 'package:pornhub_app/generate/app_image_path.dart';
import 'package:pornhub_app/model/sys_partner_cell_model/sys_partner_cell_model.dart';
import 'package:pornhub_app/utils/ad_jump.dart';
import 'package:pornhub_app/utils/color.dart';
import 'package:pornhub_app/utils/enum.dart';

import 'app_recommend_page_controller.dart';

///应用推荐
class AppRecommendPage extends GetView<AppRecommendPageController> {
  const AppRecommendPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _buildBodyView(),
    );
  }

  _buildBodyView() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 14.w),
      child: SingleChildScrollView(
        child: Column(
          children: [
            20.verticalSpace,
            const AdBanner(adress: AdPlaceNameEnum.NDD_BANNER),

            ///官方推荐
            20.verticalSpace, _buildOfficialRecommendationView(),

            ///热门应用
            20.verticalSpace, _buildPopularAppsView(),
          ],
        ),
      ),
    );
  }

  _buildTileView(String title) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Container(
          color: COLOR.color_B940FF,
          width: 4.w,
          height: 17.w,
        ),
        6.horizontalSpace,
        TextView(
          text: title,
          color: COLOR.white,
          fontSize: 16.w,
          fontWeight: FontWeight.w700,
        ),
      ],
    );
  }

  _buildOfficialRecommendationView() {
    return Column(
      children: [
        _buildTileView("官方推荐"),
        12.verticalSpace,
        Obx(() => controller.recommends.isEmpty
            ? const NoData()
            : GridView.builder(
                shrinkWrap: true,
                physics: const NeverScrollableScrollPhysics(),
                padding: EdgeInsets.only(top: 14.w),
                gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 5,
                  childAspectRatio: 60 / 80,
                ),
                itemCount: controller.recommends.length,
                itemBuilder: (BuildContext context, int index) {
                  return _buildOfficialRecommendationItemView(
                      controller.recommends[index]);
                },
              ))
      ],
    );
  }

  _buildOfficialRecommendationItemView(SysPartnerCellModel model) {
    return GestureDetector(
      onTap: () {
        jumpExternalAddress(model.apkLink ?? "", null);
      },
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          ImageView(
            src: model.icon ?? '',
            width: 60.w,
            height: 60.w,
            borderRadius: BorderRadius.circular(5.w),
          ),
          3.verticalSpace,
          TextView(
            text: model.name ?? '',
            color: COLOR.white,
            fontSize: 12.w,
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
            fontWeight: FontWeight.w500,
          ),
        ],
      ),
    );
  }

  _buildPopularAppsView() {
    return Column(
      children: [
        _buildTileView("热门应用"),
        12.verticalSpace,
        Obx(() => controller.populars.isEmpty
            ? const NoData()
            : ListView.builder(
                shrinkWrap: true,
                itemCount: controller.populars.length,
                padding: EdgeInsets.zero,
                itemBuilder: (BuildContext context, int index) {
                  return _buildPopularAppsItemView(controller.populars[index]);
                },
              )),
      ],
    );
  }

  _buildPopularAppsItemView(SysPartnerCellModel model) {
    return Row(
      children: [
        ImageView(
          src: model.icon ?? '',
          width: 60.w,
          height: 60.w,
          borderRadius: BorderRadius.circular(5.w),
        ),
        7.horizontalSpace,
        TextView(
          text: model.name ?? '',
          color: COLOR.white,
          fontSize: 16.w,
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
          fontWeight: FontWeight.w500,
        ),
        const Spacer(),
        AppBgView(
          text: "立即下载",
          imagePath: AppImagePath.btn_bg_2,
          padding: EdgeInsets.symmetric(horizontal: 8.w),
          height: 25.w,
          radius: 13.w,
          onTap: () {
            jumpExternalAddress(model.apkLink ?? "", null);
          },
        ),
      ],
    ).paddingOnly(bottom: 20.w);
  }
}
